package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.MatteBorder;
import DataObjects.Bon;

/**
 * 
 * @author igor
 * This class creates panel that will show the current order(bon) 
 * from chosen table.This panel consists of other panels, then each
 * panel is the row of the bon . 
 *
 */
public class BonPanel extends JPanel {
	
	private Vector<Bon> orders;
	private PfPtihatShulhanLeftRigtPanels leftrightP;
	private PfPtihatShulhanRecipesToChoose itemToChoosePanel;
	private BonPanel bp;
	private int flag=0 ;              //flag=1 -> PfPtihatShulhanLeftRigtPanels     ,        flag=2->  PfPtihatShulhanRecipesToChoose
	
	public BonPanel (Vector<Bon> ord,PfPtihatShulhanLeftRigtPanels lrP)
	//public BonPanel (Vector<Bon> ord)
	{
		super();
		this.setBackground(new Color(248, 248, 255));
		this.orders=ord;
		this.leftrightP=lrP;
		this.flag=1;
		//this.setMaximumSize(new Dimension(580,550));
		//this.setMinimumSize(new Dimension(580,550));
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		//this.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//this.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		//this.setLayout(null);
		initialize();
		bp=this;
	}
	
	
	public BonPanel (Vector<Bon> ord,PfPtihatShulhanRecipesToChoose lrP)
	//public BonPanel (Vector<Bon> ord)
	{
		super();
		this.setBackground(new Color(248, 248, 255));
		this.orders=ord;
		this.itemToChoosePanel=lrP;
		this.flag=2;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		initialize();
		bp=this;
	}
	
	/**
	 * This method adds rows-panels to the bon.
	 */
	private void initialize()
	{
		for(int i=0 ; i<orders.size();i++)
		{
			this.add(getOneOrderPanel(orders.get(i)));			
		}
	}
	
	
	/**
	 * creates JPanel for one single order of the table
	 * 
	 * @param oneRow is object of Class Bon that includes data about single order from a table 
	 * @return JPanel with the specific data for single order of the table.
	 */
	private JPanel getOneOrderPanel(Bon or)
	{
		JPanel oneOrder=new JPanel();
		final Bon oneRow=or;
		oneOrder.setBackground(new Color(248, 248, 255));
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		
		double width = screenSize.getWidth()/2.5;
		Double wd=new Double(width);
		int w=wd.intValue();
		
		double height = screenSize.getHeight()/12.8;
		Double dh=new Double(height);
		int h=dh.intValue();
		
		oneOrder.setPreferredSize(new Dimension(w,h));
		oneOrder.setMinimumSize(new Dimension(w,h));
		oneOrder.setMaximumSize(new Dimension(w,h));
		
		oneOrder.setLayout(new BorderLayout(5,5));
		oneOrder.setOpaque(true);
		JLabel labelName=new JLabel(oneRow.getMname());
		JLabel labelPrice=new JLabel(oneRow.getPrice());
		JLabel labelComment=new JLabel(oneRow.getComments());
		JLabel labelMana=new JLabel(oneRow.getRname());
		
		labelName.setFont(new Font("Tahoma", Font.BOLD, 15));
		labelPrice.setFont(new Font("Tahoma", Font.PLAIN, 16));
		labelComment.setFont(new Font("Tahoma", Font.PLAIN, 11));
		labelMana.setFont(new Font("Tahoma", Font.PLAIN, 11));
		
		labelMana.setHorizontalAlignment(JLabel.CENTER);
		labelName.setHorizontalAlignment(JLabel.CENTER);
		labelPrice.setHorizontalAlignment(JLabel.CENTER);
		labelComment.setHorizontalAlignment(JLabel.CENTER);
		
		
		JButton cancelBut=new JButton("ביטול");
		cancelBut.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent e) {
				JButton butt=(JButton) e.getSource();
               orders.remove(oneRow);
               //((PfPtihatShulhanLeftRigtPanels)butt.getParent().getParent().getParent()).setBones(orders);
               if(flag==1)
               {
            	   leftrightP.setBones(orders); 
               }
               
               if(flag==2)
               {
            	   itemToChoosePanel.setBones(orders); 
               }
               
               
               
			}
		});
		
		oneOrder.add(labelMana,BorderLayout.CENTER);
		oneOrder.add(labelName,BorderLayout.NORTH);
		oneOrder.add(labelPrice,BorderLayout.WEST);
		//oneOrder.add(labelComment,BorderLayout.SOUTH);
		oneOrder.add(cancelBut,BorderLayout.EAST);

		oneOrder.setBorder(new MatteBorder(0, 0, 1, 0, (Color) new Color(0, 0, 0)));
		return oneOrder;
	}
	
	

}
